import requests
import json
import time

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
    "cookie": "BDqhfp=%E7%8C%AB%26%260-10undefined%26%260%26%261; PSTM=1724382685; BIDUPSID=3F944427B3D178BFD6167E9799AD457C; ZFY=u5sMp:AvPTIA:BdTytmCUQ9ZhraJ71Ow3DNTlpihtjsBI:C; BAIDUID=D61623AC05A68D50A7ECE67B8D5930B2:FG=1; BAIDUID_BFESS=D61623AC05A68D50A7ECE67B8D5930B2:FG=1; H_PS_PSSID=60271_60360_60621_60629_60663_60677_60672; H_WISE_SIDS_BFESS=60271_60360_60621_60629_60663_60677; BA_HECTOR=a085ah2la5ah2l0k2121002g9ro8sq1jciui21u; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_WISE_SIDS=60629_60672; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; userFrom=null; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; ab_sr=1.0.1_ZGZkMDE4NDRjMTZmNDlkMTk2NjYzZjhjNWQ0NjllNzU5ZjVjYzNkZTIyZDgyYzU4ZDY3N2JlYjlkMTQxOGE4OTk3Yjk4NDc0NTEyNWUwYTE1NGJmMmI1NmM5NjliMmE4ZmZkMTk5MjNlN2RjODRmMTk1MzRlMmJlMDE3NDNmZDBiYTE3Yzk5ODJlNTA2NzhmNDQ3YTdkMDFkY2MxMGE5Mw=="
}

url = "https://image.baidu.com/search/acjson?tn=resultjson_com&logid=7598130091995986654&ipn=rj&ct=201326592&is=&fp=result&fr=&word=%E7%8C%AB&queryWord=%E7%8C%AB&cl=2&lm=&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=&copyright=&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&expermode=&nojc=&isAsync=&pn=60&rn=30&gsm=3c&1724489180017="

# 获取图片列表
response = requests.get(url=url, headers=headers)

json_obj = json.loads(response.text)

name = 0

for data in json_obj["data"]:
    name += 1

    if "replaceUrl" not in data:
        continue

    img_url = data["replaceUrl"][0]["ObjURL"]

    if len(img_url) == 0:
        continue

    print(img_url)

    # 捕获一场
    try:
        # 获取图片内容
        img_response = requests.get(img_url, headers=headers)

        if img_response.status_code != 200:
            continue

        # 将图片二进制数据保存到文件中
        with open(f"../../data/img/{name}.jpg", mode="wb") as f:
            f.write(img_response.content)
    except Exception as e:
        print("获取图片失败")
